define([
    'jQuery',
    'when',
    // Load the original Kendo source file
    'libs/kendo/kendo.web.min'
], function($, when){
    
    
    /**
     * kendo.data.ObservableArray.prototype.toArray
     * Will return a normal Javascript Array of the items present in the Observable.
     * 
     * @author      Jeroen Sen <jeroensen@22vision.nl>
     * @version     20120915
     * @param       void
     * @return      Array
     * 
     */
    kendo.data.ObservableArray.prototype.toArray = function() {
        
        var data = new Array();
        
        for (var i =0; i < this.length; i++) {
            data.push(this[i]);
   
        }
        
        return data;
        
    } // kendo.data.ObservableArray.prototype.toArray = function()
    
    
    
    /**
     * 
     * 
     */
    kendo.data.ObservableArray.prototype.toIntArray = function() {
        
        var data = new Array();
        
        for (var i=0; i < this.length; i++) {
            data.push(parseInt(this[i], 10));
        }
        
        return data;
        
    } // kendo.data.ObservableArray.prototype.toIntArray
    
    
    
    kendo.data.DataSource.prototype.sync = function() {

        var that = this,
            idx,
            length,
            created = [],
            updated = [],
            destroyed = that._destroyed,
            data = that._flatData(that._data);

        if (!that.reader.model) {
            return;
        }

        for (idx = 0, length = data.length; idx < length; idx++) {
            if (data[idx].isNew()) {
                created.push(data[idx]);
            } else if (data[idx].dirty) {
                updated.push(data[idx]);
            }
        }

        var promises = that._send("create", created);

        promises.push.apply(promises ,that._send("update", updated));
        promises.push.apply(promises ,that._send("destroy", destroyed));

        $.when.apply(null, promises)
            .then(function() {
                var idx,
                length;

                for (idx = 0, length = arguments.length; idx < length; idx++){
                    that._accept(arguments[idx]);
                }

                that._change();
                
            });
        
        return promises[0];
            
    }
    
    
    
    /**
     * 
     * 
     * 
     */
    kendo.data.DataSource.prototype.create = function(data) {

        this.add(data);
        
        return this.sync();
        
    } // kendo.data.DataSource.prototype.create = function(data)
    
    
    
    // Tell Require.js that this module returns a reference to Kendo
    return kendo;
    
    
    
});